/**
 * 
 */
package com.wzl.qs;

import java.io.FileNotFoundException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wzl.qs.db.ENote;

/**
 * @author g0800981 This class programatically creates views in list view.
 */
public class NoteAdapterView extends LinearLayout {
	private ENote myNote;

	public NoteAdapterView(Context context, ENote note)
			throws FileNotFoundException {
		super(context);
		myNote = note;
		this.setOrientation(HORIZONTAL);
		// Image view params and control
		LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(
				100, LayoutParams.WRAP_CONTENT);
		imageParams.setMargins(1, 1, 1, 1);
		ImageView imageview = new ImageView(context);
		if (note.getThumberNail().length() > 0) {
			Bitmap bm = BitmapUtil.getBitmap(note.getThumberNail(), 64);
			imageview.setImageBitmap(bm);
		} else {
			// todo: show a default image
//			imageview.setImageDrawable(R.drawable.icon);
//			Bitmap bm = Bitmap.
		}
		// imageview.set
		// Drawable
		// imageview.setBackgroundDrawable(R.drawable.nosmoking);
		addView(imageview, imageParams);

		// Add a new linear layout
		LinearLayout ll = new LinearLayout(context);
		ll.setOrientation(VERTICAL);

		// Name text view
		LinearLayout.LayoutParams nameParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		nameParams.setMargins(1, 1, 1, 1);
		TextView nameControl = new TextView(context);
		nameControl.setText(note.getName());
		nameControl.setTextSize(14f);
		nameControl.setTextColor(Color.WHITE);
		ll.addView(nameControl, nameParams);

		// Desc text view
		LinearLayout.LayoutParams descParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		descParams.setMargins(1, 1, 1, 1);
		TextView descControl = new TextView(context);
		descControl.setText(note.getDescription());
		descControl.setTextSize(14f);
		descControl.setTextColor(Color.WHITE);
		ll.addView(descControl, descParams);

		this.addView(ll);
	}

	public NoteAdapterView(Context context, ENote note, int isFavorite)
			throws FileNotFoundException {
		super(context);
		myNote = note;
		this.setOrientation(HORIZONTAL);
		int width = 150;
		int height = 100;
		// Image view params and control
		LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(
				width, height);
		imageParams.setMargins(1, 1, 1, 1);
		ImageView imageview = new ImageView(context);
		if (note.getThumberNail().length() > 0) {
			Bitmap bm = BitmapUtil.getBitmap(note.getThumberNail(), 64);
			imageview.setImageBitmap(bm);
		} else {

		}

		addView(imageview, imageParams);

		LinearLayout ll = new LinearLayout(context);
		ll.setOrientation(VERTICAL);

		// Name text view
		LinearLayout.LayoutParams nameParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		nameParams.setMargins(1, 1, 1, 1);
		TextView nameControl = new TextView(context);
		nameControl.setText(note.getName());
		nameControl.setTextSize(14f);
		nameControl.setTextColor(Color.WHITE);
		ll.addView(nameControl, nameParams);

		// Desc text view
		LinearLayout.LayoutParams descParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		descParams.setMargins(1, 1, 1, 1);
		TextView descControl = new TextView(context);
		descControl.setText(note.getDescription());
		descControl.setTextSize(14f);
		descControl.setTextColor(Color.WHITE);
		ll.addView(descControl, descParams);

		// add popularity
		LinearLayout.LayoutParams popParams = new LinearLayout.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		popParams.setMargins(1, 1, 1, 1);
		TextView popControl = new TextView(context);
		popControl.setText("viewed " + note.getPopularity()+" times");
		popControl.setTextSize(14f);
		popControl.setTextColor(Color.WHITE);
		ll.addView(popControl, popParams);

		this.addView(ll);
	}

	public ENote getENote() {
		return myNote;
	}
}
